package conchmusic.conchmusicclient.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import java.util.ArrayList;

import conchmusic.conchmusicclient.pojo.HistoryInfo;

/**
 * Created by Administrator on 2017/4/18.
 */

public class HistoryResolve {
    private SharedPreferences sp;
    private Context context;
    private ArrayList<HistoryInfo> hisInfosList;
    private  String save_history;
    private String onlineSaveHistory;
    private ArrayList<HistoryInfo> onlinehisInfosList;
    private String downloadHistory;
    private ArrayList<HistoryInfo> downloadInfosList;
    public HistoryResolve(Context context){
        this.context=context;
        this.sp = context.getSharedPreferences("history_strs", 0);
        save_history = sp.getString("history", "");
        onlineSaveHistory=sp.getString("onlineHistory","");
        downloadHistory=sp.getString("downloadHistory","");
    }
    public  ArrayList<HistoryInfo> getHisArrayList(){
        hisInfosList=new ArrayList<>();
        if(save_history.equals("")){
            Log.i("history","null");
        }else {
            String[] hisArrays = save_history.split(",");
            for(String a:hisArrays){
                String[] info=a.split("#");
                HistoryInfo historyInfo=new HistoryInfo(info[0],info[1]);
                this.hisInfosList.add(historyInfo);
            }
        }
        return hisInfosList;
    }
    public void saveHistory(String historyText,String info){
        getHisArrayList();
        for(HistoryInfo a:hisInfosList){
            if(a.getHistory().equals(historyText) && a.getInfo().equals(info)){
                return;
            }
        }
        StringBuilder sb = new StringBuilder(save_history);
        sb.append(historyText +"#"+info+ ",");
        sp.edit().putString("history", sb.toString()).commit();
    }
    public void clearHistory(){
        StringBuilder sb = new StringBuilder(save_history);
        sp.edit().clear().commit();
    }
    public ArrayList<HistoryInfo> getOnlineHistory(){
        onlinehisInfosList=new ArrayList<>();
        if(onlineSaveHistory.equals("")){
            Log.i("history","null");
        }else {
            String[] onlineHisArrays = onlineSaveHistory.split(",");
            for(String a:onlineHisArrays){
                String[] info=a.split("#");
                HistoryInfo historyInfo=new HistoryInfo(info[0],info[1]);
                this.onlinehisInfosList.add(historyInfo);
            }
        }
        return onlinehisInfosList;
    }
    public void saveOnlineHistory(String historyText,String info){
        getOnlineHistory();
        for(HistoryInfo a:onlinehisInfosList){
            if(a.getHistory().equals(historyText) && a.getInfo().equals(info)){
                return;
            }
        }
        Log.i("historyText82",historyText);
        StringBuilder sb = new StringBuilder(onlineSaveHistory);
        sb.append(historyText +"#"+info+ ",");
        sp.edit().putString("onlineHistory", sb.toString()).commit();
    }
    public void clearOnlineHistory(){
        StringBuilder sb = new StringBuilder(onlineSaveHistory);
        sp.edit().clear().commit();
    }
    public ArrayList<HistoryInfo> getDownloadHistory(){
        downloadInfosList=new ArrayList<>();
        if(downloadHistory.equals("")){
            Log.i("history","null");
        }else {
            String[] onlineHisArrays = downloadHistory.split(",");
            for(String a:onlineHisArrays){
                String[] info=a.split("#");
                HistoryInfo historyInfo=new HistoryInfo(info[0],info[1]);
                this.downloadInfosList.add(historyInfo);
            }
        }
        return downloadInfosList;
    }
    public void saveDownloadHistory(String historyText,String info){
        getDownloadHistory();
        for(HistoryInfo a:downloadInfosList){
            if(a.getHistory().equals(historyText) && a.getInfo().equals(info)){
                return;
            }
        }
        StringBuilder sb = new StringBuilder(downloadHistory);
        sb.append(historyText +"#"+info+ ",");
        sp.edit().putString("downloadHistory", sb.toString()).commit();
    }
    public void clearDownloadHistory(){
        StringBuilder sb = new StringBuilder(downloadHistory);
        sp.edit().clear().commit();
    }
}
